package com.paradogs.generator.module;

import com.gitee.sunchenbin.mybatis.actable.utils.ClassScaner;
import com.paradogs.common.utils.StringUtils;
import com.paradogs.gs.misc.annotations.CodeAutoGeneratorModule;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.apache.commons.io.FileUtils;
import org.springframework.core.annotation.AnnotationUtils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.*;

/**
 * @author: yumi
 * @date: 2025/2/20  14:28
 * @Description: TODO
 */
public class CodeAutoGeneratorModuleProcesser {
    public static void main(String[] args) throws IOException {

        final String BASE_DIR = "paradogs-gs\\src\\main\\java\\com\\paradogs\\gs\\modules";

        // 扫描 @CodeAutoGeneratorModule 修饰的实体集合
        Set<Class> scanResult = ClassScaner.scan("com.paradogs", CodeAutoGeneratorModule.class);
        // 按模块名分组
        Map<String, List<Class>> moduleMap = new HashMap<>();
        for (Class clz : scanResult) {
            CodeAutoGeneratorModule anno = AnnotationUtils.getAnnotation(clz, CodeAutoGeneratorModule.class);
            String key = StringUtils.isNotBlank(anno.value()) ? anno.value() : clz.getSimpleName();
            if (!moduleMap.containsKey(key)) moduleMap.put(key, new ArrayList<>());
            moduleMap.get(key).add(clz);
        }
        // 生成对应文件
        for (Map.Entry<String, List<Class>> entry : moduleMap.entrySet()) {
            String moduleName = entry.getKey();
            List<Class> classes = entry.getValue();
            for (Class clz : classes) {
                CodeAutoGeneratorModule anno = AnnotationUtils.getAnnotation(clz, CodeAutoGeneratorModule.class);
                String className = clz.getSimpleName();
                FileUtils.forceMkdir(new File(String.format("%s\\%s", BASE_DIR, moduleName)));
                FileUtils.forceMkdir(new File(String.format("%s\\%s\\dao", BASE_DIR, moduleName)));

                // Dao
                tryGen("dao.ftl",
                        new File(String.format("%s\\%s\\dao\\%sDao.java", BASE_DIR, moduleName, className)),
                        Map.of("moduleName", moduleName, "className", className));

                // Handler
                tryGen("handler.ftl",
                        new File(String.format("%s\\%s\\%sHandler.java", BASE_DIR, moduleName, StringUtils.firstToUpperCase(moduleName))),
                        Map.of("moduleName", moduleName, "moduleName2", StringUtils.firstToUpperCase(moduleName)));

                // GameMgr
                if (anno.mgr()) {
                    tryGen("mgr.ftl",
                            new File(String.format("%s\\%s\\%sMgr.java", BASE_DIR, moduleName, className)),
                            Map.of("moduleName", moduleName, "className", className));
                }

            }
        }

        System.out.println("Success ...");

    }

    public static void tryGen(String ftl, File output, Map<String, Object> dataModel) {
        try {
            if (output.exists()) return;                // 如果文件已存在就不生成了
            Configuration cfg = new Configuration(Configuration.VERSION_2_3_31);
            cfg.setDirectoryForTemplateLoading(new File("paradogs-generator\\src\\main\\resources\\module")); // 设置模板加载路径
            cfg.setDefaultEncoding("UTF-8");            // 设置默认编码
            Template template = cfg.getTemplate(ftl);   // 加载模板文件
            Writer out = new FileWriter(output);        // 定义输出文件
            template.process(dataModel, out);           // 合并模板和数据模型
            out.close();                                // 关闭输出流
        } catch (Exception e) {}
    }

}
